package ssl.speech;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Conversation {

	String team;
	ArrayList<Line> lines = new ArrayList<Line>();
	String outputFile = null;

	public Conversation(String filename) {
		
		
		outputFile = "tagged" + filename.substring(filename.lastIndexOf('/'));
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;

		try {
			db = dbf.newDocumentBuilder();
			Document dom = db.parse(new File(filename));

			Element root = dom.getDocumentElement();

			NodeList nodeList = root.getElementsByTagName("Utterance");

			for (int i = 0; i < nodeList.getLength(); i++) {

				Element utt = (Element) nodeList.item(i);

				int id = Integer.parseInt(utt.getAttribute("genid"));
				int ref = Integer.parseInt(utt.getAttribute("ref"));

				lines.add(new Line(id, ref, utt.getAttribute("tag"), utt
						.getTextContent()));
			}

		} catch (ParserConfigurationException e) {
			System.err.println("Parser configuration exception...");
			e.printStackTrace();
		} catch (SAXException e) {
			System.err.println("SAX Exception");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("IO Exception");
			e.printStackTrace();
		}
	}

	public ArrayList<Line> getLines() {
		return lines;
	}

	public void save() {

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();

			Document doc = db.newDocument();
			Element root = doc.createElement("Dialog");

			root.setAttribute("team", team);
			doc.appendChild(root);
			for (Line l : lines) {
				Element utt = doc.createElement("Utterance");

				utt.setAttribute("genid", new Integer(l.id).toString());
				utt.setAttribute("tag", l.tag);
				utt.setAttribute("ref", new Integer(l.refId).toString());
				utt.setTextContent(l.text);

				root.appendChild(utt);

			}

			// set up a transformer
			TransformerFactory transfac = TransformerFactory.newInstance();
			Transformer trans;

			trans = transfac.newTransformer();

			trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");

			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			DOMSource source = new DOMSource(doc);
			trans.transform(source, result);
			String xmlString = sw.toString();

			BufferedWriter output = new BufferedWriter(new FileWriter(
					outputFile));
			output.write(xmlString);
			output.flush();
		} catch (TransformerConfigurationException e) {
			System.err.println("Transformer confirguration Exception");
			e.printStackTrace();
		} catch (TransformerException e) {
			System.err.println("Transformer Exception");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("IO Exception");
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			System.err.println("Parser Configuration Exception");
			e.printStackTrace();
		}
	}

	public void setTag(int genid, String tag) {
		for (Line l : lines) {
			if (l.id == genid)
				l.setTag(tag);
		}
	}

	public String getSavedFile() {
		return outputFile;
	}
}
